// shopPages/boosradar/boosradar.js
import * as echarts from '../ec-canvas/echarts';

const app = getApp();
var startDay = ''
var endDay = ''
var dateType ='ALL_DAY'
var canvastest = '';
var widthtest = '';
var heighttest = '';

function initChart(canvas, width, height) {
  canvastest = canvas;
  widthtest = width;
  heighttest = height;
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);
  var that = this;
 
  wx.request({
    url: app.globalData.bossradar +'/api/1.0/getConversionRate',
    data: {
      uid: app.globalData.userInfo.id,
      isBoss: true,
      dateType: dateType,
      startDay: '',
      endDay: ''
    },
    method: 'POST',
    header: {
      'content-type': 'application/json' // 默认值
    },
    success(res) {


      if (res.data.status == 500) {

      } else {
        var option = {
          // title: {
          //   text: '漏斗图',
          //   subtext: '纯属虚构'
          // },
          color: ["#37A2DA", "#32C5E9", "#67E0E3"],
          tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c}%"
          },
          toolbox: {
            feature: {
              //  dataView: { readOnly: false },
              restore: {},
              saveAsImage: {}
            }
          },
          legend: {
            "textStyle": {
              "fontSize": 16
            },
            data: ['潜在客户' + res.data.data.potentialUserCountResult, '意向客户' + res.data.data.intentionalUserCountResult, '客户' + res.data.data.customerUserCountResult]
          },
          calculable: true,
          series: [
            {
              name: '漏斗图',
              type: 'funnel',
              width: '40%',
              height: '70%',
              left: '10%',

              "textStyle": {
                "fontSize": 16
              },
              width: '80%',
              // height: {totalHeight} - y - y2,
              min: 0,
              //max: 100,
              minSize: '0%',
              maxSize: '100%',
              sort: 'descending',
              //  gap: 2,
              label: {
                show: true,
                position: 'inside'
              },
              labelLine: {
                length: 10,
                orient: 'vertical',
                // lineStyle: {
                //   width: 1,
                //   type: 'solid'
                // }
              },
              itemStyle: {
                borderColor: '#fff',
                borderWidth: 1
              },
              emphasis: {
                label: {
                  fontSize: 12,
                  show: true,
                  position: 'inside'
                }
              },
              data: [
                { value: res.data.data.potentialUserCountResult, name: '潜在客户' + res.data.data.potentialUserCountResult},
                { value: res.data.data.intentionalUserCountResult, name: '意向客户' + res.data.data.intentionalUserCountResult },
                { value: res.data.data.customerUserCountResult, name: '客户' + res.data.data.customerUserCountResult }
              ]
            }
          ]
        };
      }
      chart.setOption(option);
      return chart;
    }
  })
}

var canvastest1 = '';
var widthtest1 = '';
var heighttest1 = '';
function initChart1(canvas, width, height, data) {

  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  console.log(width);
  console.log(height);

  canvastest1 = canvas
  widthtest1 = width
  heighttest1 = height

  canvas.setChart(chart);
      var option = {
        backgroundColor: "#ffffff",
        color: ["#37A2DA", "#FF9F7F"],
        tooltip: {},
        "textStyle": {
          "fontSize": 12
        },
        xAxis: {
          show: false,
        },
        yAxis: {
          show: false
        },
        radar: {
          // shape: 'circle',
          indicator: [{
            name: '客户总数',
            //max: 100
          },
          {
            name: '成交订单',
           // max: 100
          },
          {
            name: '客户转化率',
           // max: 100
          },
          {
            name: '客户咨询数',
           // max: 100
          },
          {
            name: '昨日新增客户',
            //max: 100
          },
          {
            name: '跟进客户',
            //max: 100
          }
          ]
        },
        series: [{
          name: '能力',
          type: 'radar',
          width: '40%',
          height: '85%',
          left: '30%',
          top: '10%',
          symbol: "none",
          label: {
            normal: {
              fontSize: 12,
              rich: {}
            }
          },
          "textStyle": {
            "fontSize": 12
          },
          data: [{
            value: data,
            itemStyle: {
              normal: {
                areaStyle: {
                  type: 'default',
                  opacity: 0.8, // 图表中各个图区域的透明度
                  color: "#6eaf97" // 图表中各个图区域的颜色
                }
              }
            },
            name: '能力'
          }
          ]
        }]
      };

      chart.setOption(option);
      return chart;
}



let chart;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    staffselect:0,
    staffselect1:0,
    selectTime:4,
    staffrankList:[],
    taskBack:false,
    start:'',
    end:'',
    isSelect: [
      {
        name: '总览',
        isSelect: true
      },
      {
        name: '员工排行',
        isSelect: false
      },
      {
        name: '合伙人排行',
        isSelect: false
      },
      {
        name: 'AI分析',
        isSelect: false
      }
    ],
    memberSelect:0,
    memberSelectList:[
      {
        name: '今日',
        isSelect: true
      },
      {
        name: '昨日',
        isSelect: true
      },
      {
        name: '本周',
        isSelect: true
      },
      {
        name: '筛选',
        isSelect: true
      },
      {
        name: '按跟进数',
        isSelect: true
      },
      {
        name: '按订单数',
        isSelect: true
      },
      {
        name: '按分享数',
        isSelect: true
      },
      {
        name: '按聊天数',
        isSelect: true
      },
    ],
    staffrankList1: [],
    memberSelectList1: [
      {
        name: '今日',
        isSelect: true
      },
      {
        name: '昨日',
        isSelect: true
      },
      {
        name: '本周',
        isSelect: true
      },
      {
        name: '筛选',
        isSelect: true
      },
    ],
    time: '2019/08/09',
    tab: ['累计', '昨日'],
    curren: 0,
    
    tabchart: ['累计', '今天', '昨日', '近七日'],
    c_curren: 0,
    ec: {
      onInit: initChart
    },
    ec1: {

      onInit: initChart1
    },
    memberList: [
      // {
      //   id: '1', name: '测试员工1', postion: '职位1', pic: '22', data: [0,1, 20, 4, 3, 5]
      // },
     ]
    // ec2: {
    //   onInit: initChart2
    // },
    // ec3: {
    //   onInit: initChart3
    // }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    var that = this;
    var day3 = new Date();
    day3.setTime(day3.getTime());
    var month = day3.getMonth() + 1;
    if (month.toString().length == 1) {
      month = '0' + month;
    }
    var day = day3.getDate();
    if (day.toString().length == 1) {
      day = '0' + day;
    }
    var start = day3.getFullYear() + "-" + month + "-01";
    var end = day3.getFullYear() + "-" + month + "-" + day;
    that.setData({
      start: start,
      end: end
    });
    dateType = 'ALL_DAY'
    that.getmyData()

//AI 分析
    wx.request({
      url: app.globalData.bossradar + '/api/1.0/staffAnalysis',
      data: {
        uid: app.globalData.userInfo.id,
        isBoss: true
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        //
        if (res.data.status==500){

        }else{

          var re_data = res.data.data;
          var mlist = []


          for (var i = 0; i < re_data.length; i++) {
           var sd=[]
          
            sd.push(re_data[i].customerScale)
           
            sd.push(re_data[i].followScale)
            sd.push(re_data[i].customerAddScale)
            sd.push(re_data[i].chatScale)
            sd.push(re_data[i].rateScale)
            sd.push(re_data[i].ordersScale)
          
            var allpd = { id: re_data[i].staffId, name: re_data[i].staffName, postion: re_data[i].staffPosition, pic: re_data[i].staffPic, data: sd }


            console.log(res.data.data);
            mlist.push(allpd)
            console.log(res.data.data);

           
          }

          that.setData({
            memberList: mlist
          })
          
          try{
            //在这里运行代码
            initChart1(canvastest1, widthtest1, heighttest1, that.data.memberList)
          }catch(e){

          }
      
        }
        
      }
    })
    //  console.log(">>>>>>"+dateType);
    this.getStaffRank();
  },


  getmyData() {
    var that = this
    wx.request({
      url: app.globalData.bossradar + '/api/1.0/getStatics',
      data: {
        uid: app.globalData.userInfo.id,
        dateType: dateType,
        startDay: startDay,
        endDay: endDay,
        isBoss: true
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        that.setData({
          newlist: res.data.data.count
        })
      },
      fail: function (res) {
        console.log(">>>>>>>>>>>失败")
      },
      complete: function (res) { },
    })
  },
//切换tab
  bindtab(e) {
    var index = e.currentTarget.id;
    this.setData({
      curren: index
    })
    if (index == 0) {
      dateType = 'ALL_DAY'
    } else if (index == 1) {
      dateType = 'YESTERDAY'
    }
    this.getmyData()
  },
  bindtab1(e) {
    var index = e.currentTarget.id;
    this.setData({
      c_curren: index
    })
    if (index == 0) {
      dateType = 'ALL_DAY'
    } else if (index == 1) {
      dateType = 'TODAY'
    } else if (index == 2) {
      dateType = 'YESTERDAY'
    } else if (index == 3) {
      dateType = 'WEEK'
    }

    initChart(canvastest, widthtest, heighttest);
  },

  bindhide() {
    var that = this
    var taskBack = that.data.taskBack
    that.setData({
      taskBack: false
    })
    dateType = 'SELECT_DAY'
    startDay = that.data.start
    endDay = that.data.end
    that.getmyData()
  },

  getCustomerList:function(e){
   // console.log(e.currentTarget.id)
    var row = e.currentTarget.id;
    wx.navigateTo({
      url: '/shopPages/Customerlist/Customerlist?flag=2&uid=' + this.data.staffrankList[row].staffId,
    })
  },
  staffxq(e){
    console.log(e)
    var staff_info = {};
    staff_info.staffname = e.currentTarget.dataset.staffname;
    staff_info.staffpic = e.currentTarget.dataset.staffpic;
    staff_info.staffid = e.currentTarget.dataset.staffid;
    wx.navigateTo({
      url: '/shopPages/staffxq/staffxq?staff=' + encodeURIComponent(JSON.stringify(staff_info))
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  changeSelect(e) {
    this.setData({ current: e.currentTarget.id })
    this.setData({ taskBack: false });
    if (e.currentTarget.id ==2){
      this.getStaffhehuo('TODAY')
    }
  },

  changeSwiper(e) {
    this.setData({ current: e.detail.current })
  },

  myHiddenCustomer: function () {
    wx.navigateTo({
      url: '/shopPages/Customerlist/Customerlist?flag=1&dateType=' + dateType + '&startDay=' + startDay + '&endDay=' + endDay + '&logType=0'+'&fromFlag=true',
    })
  },
  myIntentionCustomer: function () {
    wx.navigateTo({
      url: '/shopPages/Customerlist/Customerlist?flag=1&dateType=' + dateType + '&startDay=' + startDay + '&endDay=' + endDay + '&logType=1'+'&fromFlag=true',
    })
  },

  myCustomer: function () {
    wx.navigateTo({
      
      url: '/shopPages/Customerlist/Customerlist?flag=1&dateType=' + dateType + '&startDay=' + startDay + '&endDay=' + endDay + '&logType=2'+'&fromFlag=true',
    })
  },
  seleStaff:function(e){
    console.log(e)
    if (e.currentTarget.id < 4){
      this.setData({ staffselect: e.currentTarget.id })
    }else{
      this.setData({ selectTime: e.currentTarget.id })
    }
    console.log(this.data.selectTime)
    this.getStaffRank();
  },
  seleStaff1: function (e) {
    console.log(e)
    var that = this
    that.setData({ staffselect1: e.currentTarget.id })

    if (e.currentTarget.id == 3) {
      that.isShow()
    } else if (e.currentTarget.id == 0){
      that.getStaffhehuo('TODAY')
    }else if (e.currentTarget.id == 1) {
      that.getStaffhehuo('YESTERDAY')
    }else if (e.currentTarget.id == 2) {
      that.getStaffhehuo('WEEK')
    }
    // that.getStaffRank();
  },
  getStaffhehuo: function (dateType, startDay, endDay) {
    var that = this;
    var timeList = ['TODAY', 'YESTERDAY', 'WEEK', 'SELECT_DAY'];
    console.log(that.data.selectTime)

    wx.request({
      url: app.globalData.bossradar + '/api/1.0/partnerSort',
      data: {
        uid: app.globalData.userInfo.id, //客户id
        dateType: dateType,
        startDay: startDay,
        endDay: endDay,
        isBoss: true
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        that.setData({ staffrankList1: res.data.data })
      }
    })
  },
  // 合伙人筛选
  getData11: function () {
    var that = this
    var taskBack = that.data.taskBack
    that.setData({
      taskBack: false
    })
    that.getStaffhehuo('SELECT_DAY',that.data.start,that.data.end)
  },
  getStaffRank:function(){
    var that = this;
    var timeList = ['TODAY', 'YESTERDAY', 'WEEK','SELECT_DAY'];
    console.log(that.data.selectTime)

    // if (that.data.staffselect != 3){
    //   that.setData({start:''})
    //   that.setData({ end: '' })
    // }

    wx.request({
      url: app.globalData.bossradar +'/api/1.0/staffSort',
      data: {
        uid: app.globalData.userInfo.id, //客户id
        countType: that.data.selectTime - 3,
        dateType: timeList[that.data.staffselect],
        startDay: that.data.start,
        endDay:that.data.end,
        isBoss: true
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        that.setData({staffrankList:res.data.data})
      }
    })
  },
    
  isShow:function(){
    this.setData({ taskBack: true});
  },
  // 关闭筛选
  getData_hiden(){
    this.setData({
      taskBack: false
    })
  },
  // 筛选
  getData:function(){
    var that = this
    var taskBack = that.data.taskBack
    that.setData({
      taskBack: false
    })
  
    if (that.data.current == 0) {
      dateType = 'SELECT_DAY'
      startDay = that.data.start
      endDay = that.data.end
      that.getmyData()
    } else {
      that.setData({ taskBack: false });
      that.setData({ staffselect: 3 })
      that.getStaffRank();
    }
  },

  bindDateChange: function (e) {
    this.setData({
      start: e.detail.value
    })
  },
  bindDateChange1: function (e) {
    this.setData({
      end: e.detail.value
    })
  },


})